﻿using Microsoft.EntityFrameworkCore;
using Reader.Core.DataAccessObject;
using Reader.Core.DataAccessObject.Models;
using System.Threading.Tasks;

namespace Reader.Core.Application
{
    public class Authors
    {
        public async Task<Author> GetAuthorAsync(string name)
        {
            if (name.IsNullOrWhiteSpace())
            {
                name = "未知";
            }
            using var db = new ApplicationDBContext();
            var author = await db.Authors.SingleOrDefaultAsync(a => a.Name == name);
            if (author == null)
            {
                author = new Author { Name = name };
                await db.Authors.AddAsync(author);
                await db.SaveChangesAsync();
            }
            return author;
        }
    }
}
